#!/usr/bin/env python
import socket
import xlrd
import openpyxl
import xlwt
import sys
def openxlsx():
    print 'open the file: ', './tv_adjust.xlsx'
    data=xlrd.open_workbook('./tv_adjust.xlsx')
    table=data.sheet_by_name(u'Sheet1')
    return table

def fill_table(adjust):
    f = openpyxl.load_workbook('tv_adjust.xlsx')
    table =  f['Sheet1']
    for n in range(2,table.max_row):
        scrn =table.cell(row = n, column = 2).value 
        print 'scrn:',scrn
        print scrn, adjust[scrn] 
        table.cell(row = n,column = 7).value = adjust[scrn]['R']
        table.cell(row = n,column = 8).value = adjust[scrn]['G']
        table.cell(row = n,column = 9).value = adjust[scrn]['B']
        f.save('tv_adjust.xlsx',as_template = False)
def udp_command(addr,cmd):
    udp_port = 9761
    address = (str(addr), udp_port)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    print cmd,'***',address
    s.sendto(cmd, address)
    s.close()

def do_adjust():
    table = openxlsx()
    xrows=table.nrows
    for n in range(1,xrows):
    #R
      if table.row(n)[12].value <> 'x':
          addr=table.row(n)[2].value
          cmd=table.row(n)[12].value
          print 'sreen:',table.row(n)[0].value ,addr,'---',cmd
          udp_command(addr,cmd)
    #G
      if table.row(n)[13].value <> 'x':
          addr=table.row(n)[2].value
          cmd=table.row(n)[13].value
          print 'sreen:',table.row(n)[0].value ,addr,'---',cmd
          udp_command(addr,cmd)

    #B
      if table.row(n)[14].value <> 'x':
          addr=table.row(n)[2].value
          cmd=table.row(n)[14].value
          print 'sreen:',table.row(n)[0].value ,addr,'---',cmd
          udp_command(addr,cmd)

if __name__=="__main__":
    do_adjust()
